package top.techqi.core.tool.life

import android.app.Activity
import android.app.Application
import android.os.Bundle
import top.techqi.core.util.ThdUtil
import java.util.WeakHashMap
import java.util.concurrent.CopyOnWriteArrayList

object GroundMonitor : Application.ActivityLifecycleCallbacks {
    private val createdActivities = WeakHashMap<Activity, Long>()
    private val startedActivities = WeakHashMap<Activity, Long>()
    private val resumedActivities = WeakHashMap<Activity, Long>()

    @Volatile
    private var foreground = false
    private val listeners = CopyOnWriteArrayList<GroundListener>()

    fun init(app: Application) {
        app.registerActivityLifecycleCallbacks(this)
    }

    fun isApplicationInForeground(): Boolean {
        return startedActivities.isNotEmpty()
    }

    fun isApplicationInBackground(): Boolean {
        return startedActivities.isEmpty()
    }

    operator fun plusAssign(listener: GroundListener) {
        listeners += listener
    }

    operator fun minusAssign(listener: GroundListener) {
        listeners -= listener
    }

    override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
        createdActivities[activity] = System.currentTimeMillis()
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit

    override fun onActivityPreStarted(activity: Activity) {
        startedActivities[activity] = System.currentTimeMillis()

        notifyAppBecameForeground()
    }

    override fun onActivityStarted(activity: Activity) = Unit

    override fun onActivityPreResumed(activity: Activity) {
        resumedActivities[activity] = System.currentTimeMillis()
    }

    override fun onActivityResumed(activity: Activity) = Unit

    override fun onActivityPaused(activity: Activity) = Unit

    override fun onActivityPostPaused(activity: Activity) {
        resumedActivities.remove(activity)
    }

    override fun onActivityStopped(activity: Activity) = Unit

    override fun onActivityPostStopped(activity: Activity) {
        startedActivities.remove(activity)

        notifyAppBecameBackground()
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit

    override fun onActivityDestroyed(activity: Activity) = Unit

    override fun onActivityPostDestroyed(activity: Activity) {
        createdActivities.remove(activity)
    }

    private fun notifyAppBecameForeground() {
        if (!foreground && isApplicationInForeground()) {
            foreground = true
            listeners.forEach { it.onBecameForeground() }
        }
    }

    private fun notifyAppBecameBackground() {
        ThdUtil.postToWorkThread({
            if (foreground && isApplicationInBackground()) {
                foreground = false
                listeners.forEach { it.onBecameBackground() }
            }
        }, 500)
    }

    interface GroundListener {
        fun onBecameForeground()
        fun onBecameBackground()
    }
}
